package com.yc.demo.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import lombok.extern.slf4j.Slf4j;

/**
 * <p></p>
 *
 * @author: YuanChilde
 * @date: 2020-02-13 13:29
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020-02-13 13:29    YuanChilde     1.0        新增
 */
@Slf4j
public class HelloService implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener {

    public HelloService() {
        log.info("construct...");
    }

    @PostConstruct
    public void postConstruct() {
        log.info("@PostConstruct...");
    }

    @PreDestroy
    public void PreDestroy() {
        log.info("@PreDestroy...");
    }

    @Override
    public void destroy() throws Exception {
        log.info("DisposableBean destroy...");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("InitializingBean afterPropertiesSet...");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("ApplicationContextAware setApplicationContext...");
    }

    public void init() {
        log.info("@Bean-initMethod...");
    }

    public void destory() {
        log.info("@Bean-destroyMethod...");
    }

    /**
     * 或者不实现ApplicationListener接口，在非静态void方法上使用注解方式：@EventListener(value = {ContextRefreshedEvent.class,ContextStartedEvent.class,ContextClosedEvent.class})
     * @param event
     */
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        log.info("ApplicationListener onApplicationEvent...{}",event.getClass().getName());
    }
}
